package com.kuyou.ddhm_tv;



import androidx.fragment.app.Fragment;


import android.bluetooth.BluetoothGatt;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;


import com.clj.fastble.BleManager;
import com.clj.fastble.callback.BleGattCallback;
import com.clj.fastble.callback.BleScanCallback;
import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;
import com.clj.fastble.scan.BleScanRuleConfig;
import com.kuyou.ddhm_tv.fragment.FCategory;
import com.kuyou.ddhm_tv.fragment.FController;
import com.kuyou.ddhm_tv.view.BleDeviceDialog;
import com.kuyou.publiclibrary.base.ZRBaseActivity;

import java.util.List;

public class MainActivity extends ZRBaseActivity {

    public BleDevice bleDevice;
    public BleGattCallback bleGattCallback;
    public BleDeviceDialog dialog;

    private BleScanCallback bleScanCallback = new BleScanCallback() {
        @Override
        public void onScanFinished(List<BleDevice> scanResultList) {


        }

        @Override
        public void onScanStarted(boolean success) {

        }

        @Override
        public void onScanning(BleDevice bleDevice) {
            dialog.addItem(bleDevice);
            Log.d(getPackageName(),bleDevice.getName()+"");
        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void initView() {
        super.initView();
        dialog = new BleDeviceDialog(MainActivity.this);
        dialog.setOnDialogSelectCallback((device) -> {
            Log.d(getPackageName(),device.getMac());

            BleManager.getInstance().connect(device,bleGattCallback);
            dialog.dismiss();
        });
    }

    @Override
    public void initData() {
        super.initData();
        translateCategoryFragment(new FCategory());
        translateControllerFragment(new FController());
        translateTestingFragment(new Fragment());

        BleManager.getInstance().init(getApplication());
        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)
                .setOperateTimeout(5000);
        if(!BleManager.getInstance().isSupportBle()){
            Toast.makeText(this, "您的设备不支持BLE",Toast.LENGTH_SHORT).show();
            System.exit(0);
        };
        if(BleManager.getInstance().isBlueEnable()){
            BleManager.getInstance().enableBluetooth();

        }
        BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder()
                // 连接时的autoConnect参数，可选，默认false
                .setScanTimeOut(10000)              // 扫描超时时间，可选，默认10秒
                .build();
        BleManager.getInstance().initScanRule(scanRuleConfig);

    }


    public void translateControllerFragment(Fragment fragment) {
        super.translateFragment(fragment, R.id.fl_controller);
    }

    public void translateTestingFragment(Fragment fragment) {
        super.translateFragment(fragment, R.id.fl_testing);
    }

    public void translateCategoryFragment(Fragment fragment) {
        super.translateFragment(fragment, R.id.fl_category);
    }

    @Override
    public void translateFragment(Fragment fragment, int resId) {
        super.translateFragment(fragment, resId);
        Disconnect();
    }

    public void SearchDevice(){
        BleManager.getInstance().scan(bleScanCallback);
        dialog.show();
    }

    public void Disconnect(){
        BleManager.getInstance().disconnectAllDevice();
    }

    public void setBleGattCallback(BleGattCallback bleGattCallback){
        this.bleGattCallback = bleGattCallback;
    }


}